package com.aliyun.oss.hash;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Random;

public class HashConditionFinder {

    private static final String ALGORITHM = "SHA-256";
    private static final Random random = new Random();

    public static void main(String[] args) throws NoSuchAlgorithmException, IOException {
        String targetDir = "hashes";
        File dir = new File(targetDir);
        if (!dir.exists()) {
            dir.mkdir();
        }

        int leadingZeros1 = findHashWithLeadingZeros(1, dir);
        int leadingZeros2 = findHashWithLeadingZeros(2, dir);
        int leadingZeros3 = findHashWithLeadingZeros(3, dir);

        int maxLeadingZeros = 0;
        String contentWithMaxZeros = "";
        for (int i = 1; i <= 30 * 60; i++) { // 30分钟大约1800秒
            String content = generateRandomContent();
            String hash = computeHash(content);
            int zeros = countLeadingZeros(hash);
            if (zeros > maxLeadingZeros) {
                maxLeadingZeros = zeros;
                contentWithMaxZeros = content;
            }
            if (zeros >= 3) break; // 假设最多寻找连续3位0
            saveToFile(dir, content, hash);
        }

        System.out.println("Leading zeros 1: " + leadingZeros1 + " attempts");
        System.out.println("Leading zeros 2: " + leadingZeros2 + " attempts");
        System.out.println("Leading zeros 3: " + leadingZeros3 + " attempts");
        System.out.println("Max leading zeros in 30 minutes: " + maxLeadingZeros);
        System.out.println("Content with max leading zeros: " + contentWithMaxZeros);
    }

    private static int findHashWithLeadingZeros(int zeros, File dir) throws NoSuchAlgorithmException, IOException {
        int attempts = 0;
        while (true) {
            String content = generateRandomContent();
            String hash = computeHash(content);
            if (countLeadingZeros(hash) >= zeros) {
                saveToFile(dir, content, hash);
                return attempts;
            }
            attempts++;
        }
    }

    private static String generateRandomContent() {
        char[] chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();
        StringBuilder sb = new StringBuilder(50); // 假设文件内容长度为50
        for (int i = 0; i < 50; i++) {
            sb.append(chars[random.nextInt(chars.length)]);
        }
        return sb.toString();
    }

    private static String computeHash(String content) throws NoSuchAlgorithmException {
        MessageDigest digest = MessageDigest.getInstance(ALGORITHM);
        byte[] hashBytes = digest.digest(content.getBytes(StandardCharsets.UTF_8));
        StringBuilder sb = new StringBuilder();
        for (byte b : hashBytes) {
            sb.append(String.format("%02x", b));
        }
        return sb.toString();
    }

    private static int countLeadingZeros(String hash) {
        int count = 0;
        while (count < hash.length() && hash.charAt(count) == '0') {
            count++;
        }
        return count;
    }

    private static void saveToFile(File dir, String content, String hash) throws IOException {
        File file = new File(dir, hash + ".txt");
        try (FileWriter writer = new FileWriter(file)) {
            writer.write(content);
        }
    }
}